<template>
    <el-dialog :close-on-click-modal="false" :title="titleMap" v-model="visible" :width="400" destroy-on-close
        @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="80px" label-position="right">
            <el-row>
                <el-col :span="24">
                    <el-form-item label="开始时间" prop="startTime">
                        <el-time-select
                            style="width: 80%"
                            v-model="form.startTime"
                            :start="startTime"
                            step="00:30"
                            :end="endTime"
                            placeholder="选择时间"
                            @change="selectTimeFun"
                        >
                        </el-time-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </el-dialog>
</template>

<script>
export default {
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: '选择开始时间',
            visible: false,
            isSaveing: false,
            //表单数据
            form: {
                startTime: '',
            },
            //验证规则
            rules: {},
            teacher: null,
            startTime: '00:00',
            endTime: '23:00',
        }
    },
    mounted() {
    },
    methods: {
        //显示
        open(titleMap, row) {
            if (titleMap) {
                this.titleMap = titleMap
            }

            this.form = {
                startTime: '',
            }
            
            this.teacher = row.teacher
            this.startTime = row.startTime
            let courseType = row.courseType
            if (row.isAllWork == 1) {
                this.endTime = row.endTime
            }else {
                let endTime = row.endTime

                let startTimeArr = this.startTime.split(':')
                let startTimeHour = parseInt(startTimeArr[0])
                let startTimeMinute = parseInt(startTimeArr[1])
                let startTimeTotalMinute = startTimeHour * 60 + startTimeMinute
                
                let endTimeArr = endTime.split(':')
                let endTimeHour = parseInt(endTimeArr[0])
                let endTimeMinute = parseInt(endTimeArr[1])
                let endTimeTotalMinute = endTimeHour * 60 + endTimeMinute

                let totalMinute = endTimeTotalMinute - startTimeTotalMinute
                
                if (courseType == 1) {// 30分钟
                    if (totalMinute == 30) {
                        endTimeMinute = totalMinute
                    }
                    endTimeHour = endTimeHour - 1
                }else if (courseType == 2 || courseType == 4) {// 60分钟
                    if (totalMinute == 60) {
                        endTimeMinute = totalMinute
                    }else{
                        endTimeMinute = endTimeMinute - 30
                    }
                    endTimeHour = endTimeHour - 1
                }else if (courseType == 3) {// 120分钟
                    if (totalMinute == 120) {
                        endTimeMinute = totalMinute
                    }
                    endTimeHour = endTimeHour - 2
                }

                if (endTimeMinute == 29) {
                    endTimeMinute = 30
                }
                this.endTime = endTimeHour + ':' + endTimeMinute
            }

            this.visible = true;
            return this
        },
        selectTimeFun() {
            let data = {
                teacher: this.teacher,
                startTime: this.form.startTime,
            }
            this.$emit('success', data)
        }
    }
}
</script>

<style>
</style>
